﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cat.Domain.Entities.Resume;
using System.Web.Security;
using System.Web;
using Cat.Domain.Repositories.Resume;

namespace Cat.Domain.Services.Authentication
{
    public partial class FormsAuthenticationService : IAuthenticationService
    {
        private readonly HttpContextBase _httpContext;
        private readonly IJobseekerRepository _JobseekerRepository;
        private readonly TimeSpan _expirationTimeSpan;

        private Jobseeker _cachedJobseeker;

        public FormsAuthenticationService(HttpContextBase httpContext, IJobseekerRepository JobseekerRepository)
        {
            this._httpContext = httpContext;
            this._JobseekerRepository = JobseekerRepository;
            this._expirationTimeSpan = FormsAuthentication.Timeout;
        }

        public virtual void SignIn(Jobseeker Jobseeker, bool createPersistentCookie)
        {
            var now = DateTime.UtcNow.ToLocalTime();

            var ticket = new FormsAuthenticationTicket(
                1,
                Jobseeker.Email,
                now,
                now.Add(_expirationTimeSpan),
                createPersistentCookie,
                Jobseeker.Email,
                FormsAuthentication.FormsCookiePath);

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            cookie.Expires = now.Add(_expirationTimeSpan);
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Path = FormsAuthentication.FormsCookiePath;

            if (FormsAuthentication.CookieDomain != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }

            _httpContext.Response.Cookies.Add(cookie);
            _cachedJobseeker = Jobseeker;
        }


        public virtual void SignOut()
        {
            _cachedJobseeker = null;
            FormsAuthentication.SignOut();
        }
        public virtual Jobseeker GetAuthenticatedJobseeker()
        {
            if (_cachedJobseeker != null)
                return _cachedJobseeker;

            if (_httpContext == null ||
                _httpContext.Request == null ||
                !_httpContext.Request.IsAuthenticated ||
                !(_httpContext.User.Identity is FormsIdentity))
            {
                return null;
            }

            var formsIdentity = (FormsIdentity)_httpContext.User.Identity;
            var Jobseeker = GetAuthenticatedJobseekerFromTicket(formsIdentity.Ticket);

            if (Jobseeker != null)
            {
                _cachedJobseeker = Jobseeker;
            }

            return _cachedJobseeker;
        }

        public virtual Jobseeker GetAuthenticatedJobseekerFromTicket(FormsAuthenticationTicket ticket)
        {
            if (ticket == null)
                throw new ArgumentNullException("ticket");

            var email = ticket.UserData;

            if (String.IsNullOrWhiteSpace(email))
                return null;
            var Jobseeker = _JobseekerRepository.Find(item=>item.Email == email).FirstOrDefault();

            return Jobseeker;
        }

    }
}
